# Maintainer: Bjoern Lemke <lemke@lemke-it.com>

_realname=lfcbase
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.16.0
pkgrel=1
pkgdesc="LFC C++ base classes (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='http://www.lemke-it.com/'
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
        "${MINGW_PACKAGE_PREFIX}-ncurses")

makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")

source=("http://www.lemke-it.com/${_realname}-${pkgver}.tar.gz")

sha256sums=("9ffa9f2ba2fbd4f17234e7055eaa8ccdc771ca1c202d7407e4322fa84ea6a351")

prepare() {
  # for mingw build, we have to patch the tap-driver to filter out windows carrige returns from the test program output
  # yes, this is an ugly patch
  cat $srcdir/${_realname}-${pkgver}/build-aux/tap-driver.sh  | sed "s/LC_ALL\=C/sed \'s\/\\\\r\/\/g\'| LC_ALL=C/" > $srcdir/${_realname}-${pkgver}/build-aux/tap-driver-patch.sh 
  mv $srcdir/${_realname}-${pkgver}/build-aux/tap-driver-patch.sh $srcdir/${_realname}-${pkgver}/build-aux/tap-driver.sh 
  cd $srcdir/${_realname}-${pkgver}
}

build() {

  export CXXFLAGS="-DLFCNOCURSES -DDISABLE_SYSLOG"
  cd "$srcdir"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared

  make

  ${MINGW_PREFIX}/bin/gcc -shared \
    -Wl,--out-implib,liblfcbase.dll.a \
    -o liblfcbase-0.dll \
    ${srcdir}/build-${CARCH}/src/*.o \
    -lsystre -lws2_32 -lpthread -lstdc++
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make check
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib}
  install -m 0755 liblfcbase-0.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install -m 0644 liblfcbase.dll.a "${pkgdir}${MINGW_PREFIX}/lib"
}
